﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    class TileMap
    {
        Texture2D mapTexture;
        Texture2D lightTexture;
        //Texture2D backgroundTexture;

        RenderTarget2D lightTarget;
        RenderTarget2D mainTarget;

        Effect basicFogOfWarEffect;

        static int mapWidth = 1600;
        static int mapHeight = 896;

        GraphicsDevice graphicsDevice;

        public TileMap(GraphicsDevice graphicsDevice)
        {
            mapTexture = ResourceManager.getTexture("Flugvollur");
            lightTexture = ResourceManager.getTexture("light");
            basicFogOfWarEffect = ResourceManager.getEffect("fogOfWar");

            this.graphicsDevice = graphicsDevice;

            mainTarget = CreateRenderTarget();
            lightTarget = CreateRenderTarget();

        }

        private RenderTarget2D CreateRenderTarget()
        {
            return new RenderTarget2D(graphicsDevice, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight, 1, SurfaceFormat.Color);
        }

        public int MapHeight
        {
            get { return mapHeight; }
        }

        public int MapWidth
        {
            get { return mapWidth; }
        }

        public void beginDrawing(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Camera2d camera)
        {
            graphicsDevice.SetRenderTarget(0, mainTarget);
            graphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState, camera.get_transformation(graphicsDevice));
        }

        public void draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Camera2d camera)
        {
            
            spriteBatch.Draw(mapTexture,
                 new Rectangle(
                     0,
                     0,
                     mapWidth,
                     mapHeight),
                 Color.White);
        
            spriteBatch.Draw(ResourceManager.getTexture("Flugvel"),
             new Rectangle(
                 800,
                 400,
                 64 * 7,
                 64 * 8),
             Color.White);

        }

        public void endDrawing(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Camera2d camera)
        {
            spriteBatch.End();

            graphicsDevice.SetRenderTarget(0, null);
        }

        public void drawLight(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Camera2d camera, Vector2[] locs, int[] radius)
        {
            graphicsDevice.SetRenderTarget(0, lightTarget);
            graphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState, camera.get_transformation(graphicsDevice));
            for( int i = 0; i != radius.Length; i++)
            {
                spriteBatch.Draw(
                    lightTexture,
                    new Vector2(locs[i].X, locs[i].Y),
                    null,
                    Color.White,
                    0f,
                    new Vector2(16, 16),
                    Vector2.One * radius[i],
                    SpriteEffects.None,
                    1.0f
                );
            }
            spriteBatch.End();

            graphicsDevice.SetRenderTarget(0, null);
        }

        public void drawFogOfWar(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice, Camera2d camera)
        {
            graphicsDevice.Clear(Color.Black);

            Texture2D mainTex = mainTarget.GetTexture();
            Texture2D lightTex = lightTarget.GetTexture();

            basicFogOfWarEffect.Parameters["LightsTexture"].SetValue(lightTex);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            basicFogOfWarEffect.Begin();
            basicFogOfWarEffect.CurrentTechnique.Passes[0].Begin();

            spriteBatch.Draw(
                mainTex,
                new Rectangle(0, 0, graphicsDevice.PresentationParameters.BackBufferWidth, graphicsDevice.PresentationParameters.BackBufferHeight),
                Color.White
            );
            spriteBatch.End();

            basicFogOfWarEffect.CurrentTechnique.Passes[0].End();
            basicFogOfWarEffect.End();
        }

        public void drawGUI(SpriteBatch spriteBatch, int screenWidth, int screenHeight)
        {
            int miniMapSizeX = 180;
            int miniMapSizeY = 128;
            int miniMapBeginX = screenWidth - miniMapSizeX;
            int miniMapBeginY = screenHeight - miniMapSizeY;

            spriteBatch.Draw(ResourceManager.getTexture("White"), new Rectangle(0, screenHeight - 128, screenWidth, 128), Color.White);

            spriteBatch.Draw(ResourceManager.getTexture("Flugvollur_mini"),
             new Rectangle(
                 miniMapBeginX,
                 miniMapBeginY,
                 miniMapSizeX,
                 miniMapSizeY),
             Color.White);

            spriteBatch.Draw(ResourceManager.getTexture("Flugvel_mini"),
             new Rectangle(
                 miniMapBeginX + Convert.ToInt32((800.0 / mapWidth) * miniMapSizeX),
                 miniMapBeginY + Convert.ToInt32((400.0 / mapHeight) * miniMapSizeY),
                 Convert.ToInt32((440.0 / mapWidth) * miniMapSizeX),
                 Convert.ToInt32((490.0 / mapHeight) * miniMapSizeY)),
             Color.White);
        }
    }
}
